package com.sinodata.bsm.cicp.finder.windows.snmpfind;

import java.util.ArrayList;
import java.util.List;

import org.snmp4j.CommunityTarget;
import org.snmp4j.Snmp;

import com.sinodata.bsm.cicp.util.snmp.GetData;
import com.sinodata.bsm.cicp.util.snmp.MibDiskAndMemoEntry;
import com.sinodata.bsm.cicp.util.snmp.OMMappingInfo;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-8-26 下午3:43:37          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class DiskFinder extends SnmpFinder {
    @Override
    public List<String[]> find(Snmp snmp, CommunityTarget target) {

        /**
         * 磁盘名称，盘符
         */
        String diskName = null;

        /**
         * 磁盘页大小,单位B
         */
        int diskPageSize = -1;

        List<String[]> resultList = new ArrayList<String[]>();
        try {
            List<OMMappingInfo> diskes = GetData.getAllTableData(snmp, MibDiskAndMemoEntry.class, target);
            if (diskes.size() < 1) {
                throw new Exception(ip + " snmp发现windows磁盘异常，没有获取到windows磁盘数据");
            }
            for (OMMappingInfo mapping : diskes) {
                MibDiskAndMemoEntry disk = (MibDiskAndMemoEntry) mapping;
                if ("1.3.6.1.2.1.25.2.1.4".equals(disk.getHrStorageType())) {
                    diskName = getNameFromDesc(disk.getHrStorageDescr());
                    diskPageSize = disk.getHrStorageAllocationUnits();
                    String[] resultArray = new String[5];
                    resultList.add(resultArray);
                    resultArray[0] = ip + "_" + diskName;
                    resultArray[1] = diskName;
                    resultArray[2] = ip + "_" + diskName;
                    resultArray[3] = diskName;
                    resultArray[4] = diskPageSize + "";
                }
            }
            if (diskName == null || diskPageSize == -1) {
                throw new Exception(ip + " snmp发现windows磁盘异常，程序使用的oid没有匹配到windows磁盘相关数据");
            }
            return resultList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private String getNameFromDesc(String storageDesc) {
        int index = storageDesc.indexOf(':');
        if (index < 0) {
            return null;
        }
        String name = storageDesc.substring(0, index);
        return name;
    }

}
